/*
 * SelectComponent.java
 *
 * Created on December 6, 2005, 8:31 PM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */
package ge.ktapestry.propertyselections;

import org.apache.tapestry.BaseComponent;
import org.apache.tapestry.annotations.Persist;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;

import java.util.List;

public abstract class DualSelectComponent extends BaseComponent implements PageBeginRenderListener
{

    public abstract ComboList getParentModel();

    public abstract void setParentModel(ComboList parentModel);

    @Persist("session")
    public abstract ComboList getChildModel();

    public abstract void setChildModel(ComboList childModel);

    @Persist("session")
    public abstract IParentComboItem getParentValue();

    public abstract void setParentValue(IParentComboItem parentValue);

    @Persist("session")
    public abstract IParentComboItem getOldParentValue();

    public abstract void setOldParentValue(IParentComboItem oldParentValue);

    public abstract IComboItem getChildValue();

    public abstract void setChildValue(IComboItem childValue);

    @Persist("session")
    public abstract IComboItem getOldChildValue();

    public abstract void setOldChildValue(IComboItem oldChildValue);

    public void pageBeginRender(PageEvent event)
    {
        try
        {
            if (!event.getRequestCycle().isRewinding())
            {
                if (getParentValue() == null)
                {
                    setParentFromChild();
                }

                if (!getParentValue().equals(getOldParentValue()) && getChildValue().equals(getOldChildValue()))
                {
                    setChildValue((IComboItem) getParentValue().getComboList().getOptions().get(0));
//                setOldParentValue((IParentComboItem)getParentValue());
                }
                else
                {
                    setParentFromChild();
                    if (getParentValue() == null)
                    {
                    }
                }




//            setOldParentValue((IParentComboItem) SerializationUtils.clone((Serializable)getParentValue()));
                setOldParentValue(getParentValue());
                setOldChildValue(getChildValue());
            }
            else
            {
            }
            setChildModel(getParentValue().getComboList());
        }
        catch (Exception e)
        {
        }
    }

    public void setParentFromChild()
    {
        IComboItem child = getChildValue();
        List parents = getParentModel().getOptions();
        if (child != null)
        {
            for (int i = 0; i <
                    parents.size(); i++)
            {
                List children = ((IParentComboItem) parents.get(i)).getComboList().getOptions();
                for (int k = 0; k <
                        children.size(); k++)
                {
                    if (children.get(k).equals(child))
                    {
                        setParentValue((IParentComboItem) parents.get(i));
                    }

                }
            }
        }
        else
        {
            setParentValue((IParentComboItem) parents.get(0));
            setChildValue((IComboItem) getParentValue().getComboList().getOptions().get(0));
        }
    }
}